# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libmng
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.0.3
pkgrel=7
pkgdesc="A collection of routines used to create and manipulate MNG format graphics files (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://sourceforge.net/projects/libmng"
license=('spdx:Zlib')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
depends=("${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-zlib")
source=("https://downloads.sourceforge.net/libmng/${_realname}-${pkgver}.tar.xz"{,.asc}
         libmng-1.0.10-mingw.patch
         libmng-1.0.9-dont-leak-zlib-streams.diff
         no-undefined.patch
         libmng-2.0.3-cmake-versioning.patch
         fix-mng-version-cmake.patch
         pkg-config-extra-flags.patch)
sha256sums=('4a462fdd48d4bc82c1d7a21106c8a18b62f8cc0042454323058e6da0dbb57dd3'
            'SKIP'
            'f21bd32a9e4453e1334aa32faea3a13c6829bb247ffc4a4c48c75c58769c94e3'
            'e91c1f852fbbcecec40b365bc71c1b15346442018c92b41af0d29d32bfbbb895'
            '9ee540e6b375223285db286b0a04fc618d9f8d991bddd6a2ee1001fad16f3584'
            '47f8a007dd0187e2626955ffacc199e4c388349dc7ed61a9cb068939abdb872d'
            'ef79f6784910c751f4a4c27f962b7f7c02e39699137ac33eddfb8f5aab5dd44c'
            '260e5df844e1a4b4e7e52d7102b4fc2be41f6b61896e18409b99db518f84419b')
validpgpkeys=('8048643BA2C840F4F92A195FF54984BFA16C640F')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p0 -i "$srcdir"/libmng-1.0.9-dont-leak-zlib-streams.diff
  patch -p1 -i "$srcdir"/libmng-1.0.10-mingw.patch
  patch -p1 -i "$srcdir"/no-undefined.patch
  patch -p1 -i "$srcdir"/libmng-2.0.3-cmake-versioning.patch
  patch -p1 -i "$srcdir"/fix-mng-version-cmake.patch
  patch -p1 -i "$srcdir"/pkg-config-extra-flags.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  # Separate build static and shared to get different pkg-config files
  mkdir -p "${srcdir}/shared-${MSYSTEM}" && cd "${srcdir}"/shared-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    ${_extra_config[@]} \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_STATIC_LIBS=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
  
  mkdir -p "${srcdir}/static-${MSYSTEM}" && cd "${srcdir}"/static-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    ${_extra_config[@]} \
    -DBUILD_SHARED_LIBS=OFF \
    -DBUILD_STATIC_LIBS=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/static-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  mv "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/libmng{,-static}.pc
  
  cd "${srcdir}/shared-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
}
